/*年轻人，只管向前看，不要管自暴自弃者的话*/
package com.composetest.page.home

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.composetest.R
import com.composetest.core.model.Product
import com.composetest.core.model.ProductData
import com.composetest.page.product.ItemProduct
import com.composetest.ui.theme.ComposeTestTheme

/**
 * create by itz on 2024/10/10 15:59
 * desc :
 **/

@Composable
fun HomeRoute(toProductDetail: (String) -> Unit) {
    val viewModel: HomeViewModel = viewModel()
    HomeScreen(products = viewModel.datum, toProductDetail = toProductDetail)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(products: List<Product> = emptyList(), toProductDetail: (String) -> Unit = {}) {
    Scaffold(
        topBar = {
            MyHomeTopBar()
        },
    ) {
        LazyColumn(
            contentPadding = PaddingValues(horizontal = 7.dp),
            verticalArrangement = Arrangement.spacedBy(5.dp),
            modifier = Modifier
                .fillMaxSize()
                .padding(it)
        ) {
            items(products) { data ->
                ItemProduct(data = data, modifier = Modifier.clickable { toProductDetail(data.id) })
            }
        }
    }

}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyHomeTopBar() {
    TopAppBar(
        navigationIcon = {
            IconButton(onClick = { }) {
                Icon(
                    imageVector = Icons.Default.Menu,
                    contentDescription = null,
                    modifier = Modifier.size(30.dp)
                )
            }
        },
        title = {
            Row(
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(38.dp)
                    .clip(CircleShape)
                    .background(Color.White)
            ) {
                Icon(
                    imageVector = Icons.Default.Search,
                    contentDescription = null,
                    modifier = Modifier.size(18.dp)
                )
                Text(text = "搜索", style = TextStyle(fontSize = 12.sp))
            }
        },
        actions = {
            IconButton(onClick = { }) {
                Icon(
                    imageVector = Icons.Default.Add,
                    contentDescription = null,
                    modifier = Modifier.size(30.dp)
                )
            }
        },
        modifier = Modifier.background(colorResource(id = R.color.f7f5f4))
    )
}

@Preview
@Composable
fun HomeScreenPreview() {
    ComposeTestTheme {
        HomeScreen()
    }
}